package com.seeyon.apps.kk.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.kk.manager.FreemarkFlowManager;
import com.seeyon.apps.kk.manager.KkConfigManager;
import com.seeyon.ctp.common.po.template.CtpTemplate;
import com.seeyon.kk.utils.ValueUtils;

public class JsButtonController extends KkBaseController {
	private Map<String, String> jslink = new HashMap<String, String>();
	private String configFilePath = "linkmap/jslink.ftl";
	private FreemarkFlowManager freemarkFlowManager;
	private KkConfigManager kkConfigManager;

	public KkConfigManager getKkConfigManager() {
		return kkConfigManager;
	}

	public void setKkConfigManager(KkConfigManager kkConfigManager) {
		this.kkConfigManager = kkConfigManager;
	}
	public Map<String, String> getJslink() {
		return jslink;
	}

	public void setJslink(Map<String, String> jslink) {
		this.jslink = jslink;
	}
	public Map<String, String> getJslinkByCreate() {
		if(this.jslink==null||this.jslink.isEmpty()){
			String resultJson = this.freemarkFlowManager.fltTemplateIntoString(configFilePath, null);
			JSONObject json=JSONObject.parseObject(resultJson);
			for (Iterator<String> iterator = json.keySet().iterator(); iterator.hasNext();) {
				String key =   iterator.next();
				this.jslink.put(key, json.getString(key));
			}
		}
		return this.jslink;
	}

	public String getConfigFilePath() {
		return configFilePath;
	}

	public void setConfigFilePath(String configFilePath) {
		this.configFilePath = configFilePath;
	}

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}
	
	public ModelAndView refreshMap(HttpServletRequest request, HttpServletResponse response){
	
		this.jslink.clear();
		getJslinkByCreate();
		return this.ajaxDoneSuccess("ok");
	}
	
	public ModelAndView refreshKkConfig(HttpServletRequest request, HttpServletResponse response){
		
		ValueUtils.setConfig(kkConfigManager.createKkConfig());
		return this.ajaxDoneSuccess("ok");
	}
	
	

	/**
	 * 
	 * 用于扩展业务表单的按钮
	 */
	public ModelAndView expButton(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView(ROOT_PATH + "/serjs/button");

		return mav;
	}

	/**
	 * 用于扩展业务表单的控件
	 * 将业务表单根据预制Map跳转到指定页面，页面中为JS代码
	 */
	public ModelAndView expFormJs(HttpServletRequest request, HttpServletResponse response) throws Exception {
		 
		String link = "none";
		String formId = request.getParameter("formId");
		
		//暂存待办的数据 formId 实际传过来的是 流程ID
		//将流程ID转换为 templateId做匹配
		List<Map<String,Object>> tvl=this.jdbcTemplate.queryForList("select id,TEMPLETE_ID from col_summary where id=?",formId);
		if(!tvl.isEmpty()){
			formId=MapUtils.getString(tvl.get(0), "TEMPLETE_ID")  ; 
		} 
		
		JSONObject obj =null;
		//如果当前的templateid直接命中了映射的扩展，则直接使用
		if (StringUtils.isNotBlank(formId) && this.getJslinkByCreate().containsKey(formId)) {
			String json = this.getJslinkByCreate().get(formId);			
			obj = JSONObject.parseObject(json);
			link=obj.getString("link");			
		} 
		

		ModelAndView mav = new ModelAndView(ROOT_PATH + "/serjs/" + link);
		mav.addAllObjects(obj);
		return mav;
	}
}